package components.managers
{
    import components.contorls.superClass.BaseToolTip;
    import components.core.UIComponent;
    import components.core.zn_internal;

    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.filters.BitmapFilterQuality;
    import flash.filters.BlurFilter;
    import flash.utils.Dictionary;

    public class SystemManager extends UIComponent
    {
        public static var rootStage:Stage;

        private static var _instance:SystemManager;

        private var _compDisplay:Sprite;

        private var _viewDisplay:Sprite;

        private var _popCompLayerDisplay:Sprite;

        private var _toolTipCompLayerDisplay:Sprite;

        /**
         *提示信息显示层
         */
        private var _infoCompLayerDisplay:Sprite;

        private var _testSp:Sprite;

        public var callLaterUI:UIComponent = new UIComponent();

        private var _modalTransparencySpList:Dictionary = new Dictionary(true);

        public function SystemManager()
        {
            super();

            if (_instance)
                throw new Error("不允许新建主程序类");

            _instance = this;

            if (stage)
                addToStageHandler(null);
            else
                addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
        }

        public static function get instance():SystemManager
        {
            return _instance;
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();

            if (!_initialized)
            {
                _compDisplay = new Sprite();
                _compDisplay.mouseEnabled = false;

                _viewDisplay = new Sprite();
                _viewDisplay.mouseEnabled = false;
                _popCompLayerDisplay = new Sprite();
                _popCompLayerDisplay.mouseEnabled = false;

                _toolTipCompLayerDisplay = new Sprite();
                _toolTipCompLayerDisplay.mouseEnabled = false;
                _toolTipCompLayerDisplay.mouseChildren = false;

                _infoCompLayerDisplay = new Sprite();
                _infoCompLayerDisplay.mouseChildren = false;
                _infoCompLayerDisplay.mouseEnabled = false;

                _testSp = new Sprite();

                zn_internal::addChild(_compDisplay);
                zn_internal::addChild(_viewDisplay);
                zn_internal::addChild(_popCompLayerDisplay);
                zn_internal::addChild(_toolTipCompLayerDisplay);
                zn_internal::addChild(_infoCompLayerDisplay);
                zn_internal::addChild(_testSp);
            }
        }

        protected override function measureSizes():Boolean
        {
            if (super.measureSizes())
            {
                updateChildSize(_compDisplay);
                updateChildSize(_viewDisplay);
                updateChildSize(_popCompLayerDisplay);
                return true;
            }

            return false;
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/


        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        public static function callLater(obj:*, method:Function, args:Array = null):Boolean
        {
            if (instance == null)
                new SystemManager();

            return instance.callLaterUI.callLater(obj, method, args);
        }

        protected function addToStageHandler(event:Event):void
        {
            rootStage = stage;

            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            setSize(stage.stageWidth, stage.stageHeight);

//			x=stage.stageWidth*0.5-width*0.5;
//			y=stage.stageHeight*0.5-height*0.5;

            stage.addEventListener(Event.RESIZE, stage_resizeHandler);
        }

        protected function stage_resizeHandler(event:Event):void
        {
            setSize(stage.stageWidth, stage.stageHeight);
            updateModalTransparencySize();
        }

        public override function addChild(child:DisplayObject):DisplayObject
        {
            return addChildAt(child, _compDisplay.numChildren);
        }

        public override function addChildAt(child:DisplayObject, index:int):DisplayObject
        {
            if (child is UIComponent)
                UIComponent(child).parent = this;

            return _compDisplay.addChildAt(child, index);
        }

        public override function contains(child:DisplayObject):Boolean
        {
            return _compDisplay.contains(child);
        }

        public override function getChildAt(index:int):DisplayObject
        {
            return _compDisplay.getChildAt(index);
        }

        public override function getChildByName(name:String):DisplayObject
        {
            return _compDisplay.getChildByName(name);
        }

        public override function getChildIndex(child:DisplayObject):int
        {
            return _compDisplay.getChildIndex(child);
        }

        public override function get numChildren():int
        {
            return _compDisplay.numChildren;
        }

        public override function removeChild(child:DisplayObject):DisplayObject
        {
            if (!_compDisplay.contains(child))
                return child;

            if (child is UIComponent)
                UIComponent(child).parent = null;

            return _compDisplay.removeChild(child);
        }

        public override function removeChildAt(index:int):DisplayObject
        {
            var child:DisplayObject = _compDisplay.getChildAt(index) as DisplayObject;
            if (child)
                removeChild(child);

            return child;
        }

        zn_internal function addPopChild(child:DisplayObject):DisplayObject
        {
            if (child is UIComponent)
                UIComponent(child).parent = this;

            return _popCompLayerDisplay.addChild(child);
        }

        zn_internal function removePopChild(child:DisplayObject):DisplayObject
        {
            if (_popCompLayerDisplay.contains(child))
            {
                if (child is UIComponent)
                    UIComponent(child).parent = null;

                return _popCompLayerDisplay.removeChild(child);
            }
            return null;
        }

        /**
         *添加弹出窗口的不可以点击区域
         * @param blur
         * @return
         *
         */
        zn_internal function addModalTransparency(blur:int):Sprite
        {
            var sp:Sprite = new Sprite();
            sp.graphics.beginFill(0xDDDDDD, 0);
            sp.graphics.drawRect(0, 0, 1, 1);
            sp.graphics.endFill();

            sp.width = width;
            sp.height = height;

            _modalTransparencySpList[sp] = sp;

            _popCompLayerDisplay.addChild(sp);

            _compDisplay.filters = [ new BlurFilter(blur, blur, BitmapFilterQuality.HIGH)];

            return sp;
        }

        /**
         *移除弹出窗口的不可点击区域
         * @param blur
         * @param mc
         *
         */
        zn_internal function removeModalTransparency(blur:int, displayObject:DisplayObject):void
        {
            if (_popCompLayerDisplay.contains(displayObject))
            {
                delete _modalTransparencySpList[displayObject];

                _popCompLayerDisplay.removeChild(displayObject);

                _compDisplay.filters = [ new BlurFilter(blur, blur, BitmapFilterQuality.HIGH)];
            }
        }

        /**
         *更新弹出遮罩层的大小
         *
         */
        protected function updateModalTransparencySize():void
        {
            for each (var displayObject:DisplayObject in _modalTransparencySpList)
            {
                displayObject.width = width;
                displayObject.height = height;
            }
        }

        /**
         *显示Tips
         * @param toolTip
         *
         */
        zn_internal function showToolTip(toolTip:BaseToolTip):void
        {
            _toolTipCompLayerDisplay.addChild(toolTip);
        }

        zn_internal function hideToolTip():void
        {
            while (_toolTipCompLayerDisplay.numChildren > 0)
                _toolTipCompLayerDisplay.removeChildAt(0);
        }

        public function addChildInfo(displayObject:DisplayObject):void
        {
            _infoCompLayerDisplay.addChild(displayObject);
        }

        public function removeChildInfo(displayObject:DisplayObject):void
        {
            if (_infoCompLayerDisplay.contains(displayObject))
                _infoCompLayerDisplay.removeChild(displayObject);
        }

        public function get compDisplay():Sprite
        {
            return _compDisplay;
        }

        public function get popCompLayerDisplay():Sprite
        {
            return _popCompLayerDisplay;
        }

        public function get toolTipCompLayerDisplay():Sprite
        {
            return _toolTipCompLayerDisplay;
        }

        public function get infoCompLayerDisplay():Sprite
        {
            return _infoCompLayerDisplay;
        }

        public function addChildTest(displayObject:DisplayObject):void
        {
            _testSp.addChild(displayObject);
        }

        public function removeChildTest(displayObject:DisplayObject):void
        {
            if (_testSp.contains(displayObject))
                _testSp.removeChild(displayObject);
        }

        public function addViewDisplay(displayObject:DisplayObject):void
        {
            _viewDisplay.addChild(displayObject);
        }

        public function removeViewDisplay(displayObject:DisplayObject):void
        {
            if (_viewDisplay.contains(displayObject))
                _viewDisplay.removeChild(displayObject);
        }
    }
}
